﻿using System;
using System.Collections.Generic;
using System.Text;
using SourceCode.K2Mng;

namespace Lisphi.Lab.K2Lib
{
    public class K2Man
    {
        public string Server { get; set; }
        public int Port { get; set; }
        public string DomainName { get; set; }
        public string AdminName { get; set; }
        public string AdminPassword { get; set; }

        private K2Manager Login()
        {
            K2Manager mgr = new K2Manager();
            string conStr = string.Format("[;];Authentication=Windows;Domain={0};User={1};Password={2}", DomainName, AdminName, AdminPassword); ;
            mgr.Login(Server, Port, conStr);
            return mgr;
        }

        private void Logout(SourceCode.K2Mng.K2Manager mgr)
        {
            mgr.Logout();
        }

        public WorkListItems GetWorkListItems(string processName, string folio)
        {
            K2Manager mgr = Login();
            try
            {
                return mgr.GetWorkListItems(null, processName, null, null, folio, null, null);
            }
            finally
            {
                Logout(mgr);
            }
        }

        public object RedirectWorklistItem(int worklistItemID, string userName)
        {
            K2Manager mgr = Login();
            try
            {
                return mgr.RedirectWorklistItem(worklistItemID, userName);
            }
            finally
            {
                Logout(mgr);
            }
        }
    }
}
